const express = require("express");
const router = express.Router();
const { User, Role } = require("../models");
const { Op } = require("sequelize");
const { NotFound } = require("http-errors");
const { success, failure } = require("../utils/responses");
const role = require("../models/role");

/**
 * 查询用户列表
 * GET /admin/users
 */
router.get("/", async function (req, res) {
  try {
    // const query = req.query;
    // const currentPage = Math.abs(Number(query.currentPage)) || 1;
    // const pageSize = Math.abs(Number(query.pageSize)) || 10;
    // const offset = (currentPage - 1) * pageSize;

    // const condition = {
    //   where: {},
    //   order: [["id", "DESC"]],
    //   limit: pageSize,
    //   offset: offset,
    //   attributes: { exclude: ["password","role"] },
    //   include: [
    //     {
    //       model: Role,
    //       as: "roleInfo",
    //       attributes: ["name", "description"], // 只返回你需要的角色字段
    //     },
    //   ],
    // };
    // const { count, rows }  = await Role.findAll({
    //   include: [
    //     {
    //       model: User,
    //       as: "users",
    //       attributes: ["id", "username"],
    //     },
    //   ],
    // });

    // if (query.username) {
    //   condition.where.title = {
    //     [Op.like]: `%${query.username}%`,
    //   };
    // }
    const  { count, rows } = await Role.findAndCountAll({
      include: [
        {
          model: User,
          as: "users",
          attributes: ["id", "username"],
        },
      ],
      attributes: ["id", "name","description"],
    });
    console.log(rows);

    // const { count, rows } = await User.findAndCountAll(condition);
    success(res, "查询用户列表成功。", {
      users: rows,
      pagination: {
        total: count,
        // currentPage,
        // pageSize,
      },
    });
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 查询用户详情
 * GET /admin/users/:id
 */
router.get("/:id", async function (req, res) {
  try {
    const user = await getArticle(req);
    success(res, "查询用户成功。", { user });
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 创建用户
 * POST /admin/users
 */
router.post("/", async function (req, res) {
  try {
    const body = filterBody(req);
    console.log(body);

    const user = await User.create(body);
    success(res, "创建用户成功。", { user }, 201);
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 更新用户
 * PUT /admin/users/:id
 */
router.put("/:id", async function (req, res) {
  try {
    const user = await getArticle(req);
    const body = filterBody(req);

    await user.update(body);
    success(res, "更新用户成功。", { user });
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 删除用户
 * DELETE /admin/users/:id
 */
router.delete("/:id", async function (req, res) {
  try {
    const user = await getArticle(req);

    await user.destroy();
    success(res, "删除用户成功。");
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 公共方法：查询当前用户
 */
async function getArticle(req) {
  const { id } = req.params;

  const user = await User.findByPk(id);
  if (!user) {
    throw new NotFound(`ID: ${id}的用户未找到。`);
  }

  return user;
}

/**
 * 公共方法：白名单过滤
 * @param req
 * @returns {{title, content: (string|string|DocumentFragment|*)}}
 */
function filterBody(req) {
  return {
    username: req.body.username,
    role: req.body.role,
    password: req.body.password,
  };
}

module.exports = router;
